package com.ld.admin.controller.system.dbtool;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.codegen.CodegenDbsetModel;
import com.ld.admin.service.codegen.CodegenService;
import com.ld.shieldsb.common.composition.util.web.Global;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.JsoupUtil;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;
import com.ld.shieldsb.db.model.DBSetInfo;
import com.ld.shieldsb.db.service.DBTabelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "建表工具")
@ConditionalOnProperty(prefix = "modules.sc.system.codegen", name = "tablecreateEnabled", havingValue = "true", matchIfMissing = true)
@Controller
@RequestMapping("${shieldsb.adminPath}/system/codegen/tablecreate")
public class CodeGenTableCreateController extends AdminBasicController<CodegenDbsetModel> {
    @Autowired
    public CodegenService codegenService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        // 搜索数据库设置
        List<CodegenDbsetModel> dbSetList = basicService.getList(CodegenDbsetModel.class, basicService.getNormalQueryModel());
        request.setAttribute("dbsets", dbSetList);
        return "a/system/codegen/tablecreate/show";
    }

    @ApiOperation(value = "创建表", notes = "")
    @RequestMapping(path = "/create", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object create() throws ServletException, IOException {
        Result result = codegenService.createTable();
        return Web.Response.result(result);
    }

    @ApiOperation(value = "预览建表语句", notes = "")
    @RequestMapping(path = "/preview", method = RequestMethod.POST)
    public String preview() throws ServletException, IOException {
        try {
            Result result = codegenService.getCreateTableSql();
            request.setAttribute("model", result.getData());
        } catch (Exception e) {
            log.error("生成建表语句失败！", e);
            return Web.Response.error("生成建表语句失败！" + e.getMessage()).toString();
        }
        return "a/system/codegen/tablecreate/preview";
    }

    @ApiOperation(value = "获取系统列设置", notes = "")
    @RequestMapping(path = "/getCols", method = RequestMethod.POST)
    // 返回json格式
//    @ResponseBody
    public Object getCols() throws ServletException, IOException {
        String name = getString("name").trim();
        QueryModel queryModel = basicService.getNormalQueryModel();
        QueryModel queryModelOr1 = new QueryModel();
        queryModelOr1.addLike("columnName", name); // 列名
        QueryModel queryModelOr2 = new QueryModel();
        queryModelOr2.addLike("showName", name); // 中文名称
        QueryModel queryModelOr3 = new QueryModel();
        queryModelOr3.addLike("aliasName", name); // 中文别名
        queryModel.addOrMultiCondition(queryModelOr1, queryModelOr2, queryModelOr3);
//        List<DbtoolColsModel> colsModels = basicService.getList(DbtoolColsModel.class, queryModel, 10);
//        request.setAttribute("list", colsModels);
        // 读取配置文件
        String getColsUrl = PropertiesModel.CONFIG.getString("getColsUrl");
        if (StringUtils.isBlank(getColsUrl)) {
            getColsUrl = Global.getProjectFrontURL() + "/dbtool/getCols?name=";
        }
        if (StringUtils.isNotBlank(getColsUrl)) {

            JSONObject json = JsoupUtil.getRemoteURLJson(getColsUrl + name);
            if (json != null) {
                request.setAttribute("list", json.get("data"));
            }
        }
//        return Web.Response.success("", colsModels);
        return "a/system/codegen/tablecreate/cols";
    }

    @ApiOperation(value = "获取字段列表", notes = "")
    @RequestMapping(path = "/getFieldList", method = RequestMethod.POST)
    @ResponseBody
    public Object getFieldList() throws ServletException, IOException {
        Result result = codegenService.getDBConn();
        if (!result.getSuccess()) {
            return returnJson(result);
        }
        DBSetInfo dbSetInfo = (DBSetInfo) result.getData();
        String dbType = DBTabelService.getDBType(dbSetInfo);
//        return "a/system/codegen/tablecreate/" + dbType + "/fieldlist";
        return forward("a/system/codegen/tablecreate/" + dbType + "/fieldlist");
    }
}
